package jobase

import (
	"gitee.com/jolib/jokode"
	"strconv"
)

func AtoI(d string) int {
	if x, err := strconv.Atoi(d); err != nil {
		jokode.Errorf("try convert string '%s' to int fail, error info: %v", d, err)
	} else {
		return x
	}

	return -1
}

func AtoI64(d string) int64 {
	if x, err := strconv.ParseInt(d, 10, 64); err != nil {
		jokode.Errorf("try convert string '%s' to int64 fail, error info: %v", d, err)
	} else {
		return x
	}

	return -1
}

func AtoF64(d string) float64 {
	if x, err := strconv.ParseFloat(d, 64); err != nil {
		jokode.Errorf("try convert string '%s' to float64 fail, error info: %v", d, err)
	} else {
		return x
	}

	return -1
}

func F64toA(d int64) string {
	return strconv.FormatInt(d, 10)
}

func BtoI(b bool) int {
	if b {
		return 1
	} else {
		return 0
	}
}

func Decimal(val float64, pre int) float64 {
	val, _ = strconv.ParseFloat(strconv.FormatFloat(val, 'f', pre, 64), 64)
	return val
}
